<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      ~(function () {
        /*
              控制异步执行变成同步 等待前面一个执行完成，才能继续执行
执行必须使用next 否则不会执行 当done为true则下面没有需要运行的      
            */
        //先输出2在输出1
        function* fn() {
          yield 1;
          yield 2;
          yield 3;
        }
        let fn2 = fn();
        console.log(fn2.next()); //{value: 1, done: false}
        console.log(fn2.next()); //{value: 2, done: false}
        console.log(fn2.next()); //{value: 3, done: false}
        console.log(fn2.next()); //{value: undefined, done: true}
        console.log(3); //3
      })();

      ~(function () {
        /*
next传参 将会作为yield前面一个的返回值 fn2.next(22)是第一个yield 1返回值     
            */
        //先输出2在输出1
        function* fn(arg) {
          console.log(arg); //1
          let a = yield 1;
          console.log(a); //22
          yield 2;
          yield 3;
        }
        let fn2 = fn(1);
        console.log(fn2.next()); //{value: 1, done: false}
        console.log(fn2.next(22)); //{value: 2, done: false}
        console.log(fn2.next()); //{value: 3, done: false}
        console.log(fn2.next()); //{value: undefined, done: true}
        console.log(3); //3
      })();

      ~(function () {
        function* gen() {
          yield 'welcome';
          yield 'to';
          yield 'hello';
          return '12';
        }
        console.log(Array.from(gen())); //输出["welcome", "to", "hello"]
      })();

      ~(function () {
        //用法
        function* gen() {
          yield 'welcome';
          yield 'to';
          yield 'hello';
          return '12';
        }
        let p = gen();
        for (let val of p) {
          console.log(val); // welcome to hello return的数据不执行
        }
      })();

      ~(function () {
        //for of可以自动循环
        function* gen() {
          yield 'welcome';
          yield 'to';
          yield 'hello';
          return '12';
        }
        let p = gen();
        for (let val of p) {
          console.log(val); // welcome to hello return的数据不执行
        }
      })();

      ~(function () {
        //结构赋值自动执行
        function* gen() {
          yield 'welcome';
          yield 'to';
          yield 'hello';
          return '12';
        }
        let [a, b, c] = gen();
        console.log(a, b, c); //welcome to hello
      })();
    </script>
  </body>
</html>
